package com.yxjx.duoxue.customview;

import java.util.Currency;

import org.htmlparser.tags.HeadTag;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.utils.Utils;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Scroller;

public class OutScrollView extends ScrollView {
	
	private static final String TAG = "OutScrollView";
	
	// 监听scroll停止
	public static final int EVENT_ID = -9983761;
	private int lastY = 0;
	
    View.OnTouchListener mGestureListener;

	private View tabs;

	public OutScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
        setFadingEdgeLength(0);
        
	}

	public OutScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public OutScrollView(Context context) {
		super(context, null);
	}
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		boolean ret = super.onInterceptTouchEvent(ev);
		return ret;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_UP) {
			handler.sendMessageDelayed(handler.obtainMessage(EVENT_ID), 5);
		}
		boolean ret = super.onTouchEvent(ev);
		return ret;
	}
	
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case EVENT_ID:
				if (lastY == getScrollY()) {
					// stop
					handleStop();
				} else {
					sendMessageDelayed(handler.obtainMessage(EVENT_ID), 5);
					lastY = getScrollY();
				}
				break;
			default:
				break;
			}
		};
	};

	protected void handleStop() {
		// 如果当前的距离顶端的距离大于阈值，并且当前tab的当前的位置高于阈值
		//，重置到tab。
		int resetThreshHold = getHeight() / 4;
		if (getScrollY() > resetThreshHold) {
			if (tabs == null) {
				tabs = findViewById(R.id.head_tabs);
				if (tabs == null) {
					throw new RuntimeException("OutScrollView should have a child named head_tabs");
				}
			}
			Utils.logd("handleStop tabBottom =" + tabs.getBottom()
					+ ", getScrollY = " + getScrollY()
					+ ", this.getHeight() = " + getHeight());
//			if (tabs.getBottom() - getScrollY() < getHeight() - resetThreshHold) {
//				postDelayed((new Runnable() {
//					
//					@Override
//					public void run() {
//						int top = tabs.getTop();
//						Utils.logd("handleStop scroll to tab pos = " + top + ", currentScrollY = " + getScrollY());
//						smoothScrollTo(0, top);
//					}
//				}), 100);
//			}
		} else {
			// do nothing
			// 否者，置顶
//			postDelayed((new Runnable() {
//				
//				@Override
//				public void run() {
//					smoothScrollTo(0, 0);
//					Utils.logd("handleStop scroll to top = " + 0 + ", currentScrollY = " + getScrollY());					
//				}
//			}), 100);
			
		}
		
	}
	
	//Tab 是否已经置顶
	public boolean isTabTop() {
		if (tabs == null) {
			tabs = (LinearLayout) findViewById(R.id.head_tabs);
		}
		return tabs.getTop() <= getScrollY();
	}
	
}

